/*
 * Copyright (C) 2010 Seapeak.Xu
 *
 * fostmq may be copied only under the terms of the GNU General
 * Public License V3, which may be found in the fostmq source kit.
 *
 * Created on: 2011-5-12
 * Author: Seapeak.Xu
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/poll.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/times.h>
#include <sys/select.h>
#include <string.h>

#include "fost_typedef.h"
#include "fost_timer.h"
#include "fost_logger.h"

int timer(int sec,int usec,elapsedhandler event,void *arg)
{
	int result = 0;
	struct timeval timespan;
	while(true)
	{
		timespan.tv_sec = sec;
		timespan.tv_usec = usec;
		result = (select(0,NULL,NULL,NULL,&timespan));
		if(0 > result)
		{
			log_error("File:%s,Line:%d,Function:%s.timer by select is error.number:%d.",
					__FILE__,__LINE__,__FUNCTION__,result);
		}
		if(0 == result)
		{
			event(arg);
		}
		else
		{
			if(!(EAGAIN == errno || EWOULDBLOCK == errno))
			{
				log_error("File:%s,Line:%d,Function:%s.send is fail.Errno:%d,Info:%s.",
						__FILE__,__LINE__,__FUNCTION__,errno,strerror(errno));
				return errno;
			}

		}
	}
	return 0;
}
